package com.xyz.aclservice.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;


import com.xyz.aclservice.entity.EduRole;
import com.xyz.aclservice.service.EduRoleService;
import com.xyz.entity.R;
import io.swagger.v3.oas.annotations.media.Schema;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author testjava
 * @since 2020-01-12
 */
@Schema(description="角色管理接口")
@RestController
@RequestMapping("/admin/acl/role")
//@CrossOrigin
public class EduRoleController {

    @Autowired
    private EduRoleService eduRoleService;

    @Schema(description="获取角色分页列表")
    @GetMapping("{page}/{limit}")
    public R index(
            @Schema(name = "page", description = "当前页码", required = true)
            @PathVariable Long page,

            @Schema(name = "limit", description = "每页记录数", required = true)
            @PathVariable Long limit,
            EduRole eduRole) {
        Page<EduRole> pageParam = new Page<>(page, limit);
        QueryWrapper<EduRole> wrapper = new QueryWrapper<>();
        if(!StringUtils.isEmpty(eduRole.getRoleName())) {
            wrapper.like("role_name", eduRole.getRoleName());
        }
        eduRoleService.page(pageParam,wrapper);
        return R.ok().data("items", pageParam.getRecords()).data("total", pageParam.getTotal());
    }

    @Schema(description="获取角色")
    @GetMapping("get/{id}")
    public R get(@PathVariable String id) {
        EduRole eduRole = eduRoleService.getById(id);
        return R.ok().data("item", eduRole);
    }

    @Schema(description="新增角色")
    @PostMapping("save")
    public R save(@RequestBody EduRole eduRole) {
        eduRoleService.save(eduRole);
        return R.ok();
    }

    @Schema(description="修改角色")
    @PutMapping("update")
    public R updateById(@RequestBody EduRole eduRole) {
        eduRoleService.updateById(eduRole);
        return R.ok();
    }

    @Schema(description="删除角色")
    @DeleteMapping("remove/{id}")
    public R remove(@PathVariable String id) {
        eduRoleService.removeById(id);
        return R.ok();
    }

    @Schema(description="根据id列表删除角色")
    @DeleteMapping("batchRemove")
    public R batchRemove(@RequestBody List<String> idList) {
        eduRoleService.removeByIds(idList);
        return R.ok();
    }
}

